package com.ylbh.modules.share.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ylbh.common.utils.*;
import com.ylbh.modules.banner.entity.AppHomeBanner;
import com.ylbh.modules.banner.mapper.AppHomeBannerMapper;
import com.ylbh.modules.share.entity.WeChatShare;
import com.ylbh.modules.share.mapper.WeChatShareMapper;
import com.ylbh.modules.share.service.WeChatShareService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.List;


@Service
public class WeChatShareServiceImpl extends ServiceImpl<WeChatShareMapper, WeChatShare> implements WeChatShareService {

    @Autowired
    private WeChatShareMapper weChatShareMapper;

    @Autowired
    private AppHomeBannerMapper appHomeBannerMapper;

    /**
     * 微信分享列表
     * @param pageDTO
     * @return
     */
    @Override
    public PageUtils queryShareList(PageDTO pageDTO) {
        Page<Object> page = new Page<>(pageDTO.getPage(), pageDTO.getLimit());
        List<WeChatShare> selectShareList = weChatShareMapper.selectShareList(pageDTO, page);
        return new PageUtils(new Page<WeChatShare>().setTotal(page.getTotal()).setRecords(selectShareList));
    }

    /**
     * 微信分享详情
     * @param id
     * @return
     */
    @Override
    public WeChatShare getShareDetailsById(String id) {
        return weChatShareMapper.selectShareDetailsById(id);
    }

    /**
     * 微信分享修改
     * @param weChatShare
     */
    @Override
    public void modifyShare(WeChatShare weChatShare) {
        weChatShare.setUpdateTime(new Date());
        weChatShareMapper.updateShareById(weChatShare);
    }

    /**
     * 新增微信分享
     * @param weChatShare
     */
    @Override
    public void addShare(WeChatShare weChatShare) {
        weChatShare.setCreateUserId(ShiroUtils.getUserId().intValue());
        weChatShare.setUpdateUserId(ShiroUtils.getUserId().intValue());
        weChatShare.setCreateTime(new Date());
        weChatShare.setUpdateTime(new Date());
        IdWorker id = new IdWorker();
        weChatShare.setWeChatId(id.nextId());
        weChatShareMapper.insertShare(weChatShare);
    }

    /**
     * 上传分享图片
     * @param file
     * @return
     */
    @Override
    public String uploadShare(MultipartFile file) {
        OSSClientUtil ossClient = null;
        try{
            ossClient = new OSSClientUtil();
            String imgUrl = ossClient.uploadImg2Oss(file,"wechat");//上传文件到服务器
            if("fail".equals(imgUrl)){
                return "上传失败";
            }
            return "http://image.yilianbaihui.cn/consumer/wechat/" + imgUrl;
        }catch (Exception e) {									//捕获异常
            e.printStackTrace();
            return "上传失败";
        }
    }

    /**
     * 删除分享
     * @param asList
     */
    @Override
    public void removeShareById(List<String> asList) {
        weChatShareMapper.deleteShareById(asList);
    }

    /**
     * 分享规则
     * @param pageDTO
     * @return
     */
    @Override
    public PageUtils queryShareRule(PageDTO pageDTO) {
        Page<Object> page = new Page<>(pageDTO.getPage(), pageDTO.getLimit());
        List<AppHomeBanner> list = appHomeBannerMapper.selectShareRule(page);
        return new PageUtils(new Page<AppHomeBanner>().setTotal(page.getTotal()).setRecords(list));
    }

    /**
     * 分享规则详情
     * @param id
     * @return
     */
    @Override
    public AppHomeBanner getShareRuleById(Integer id) {
        return appHomeBannerMapper.selectById(id);
    }

    /**
     * 修改分享规则详情
     * @param appHomeBanner
     */
    @Override
    public void modifyShareRuleById(AppHomeBanner appHomeBanner) {
        appHomeBannerMapper.updateById(appHomeBanner);
    }

    /**
     * 上传分享规则图片
     * @param file
     * @return
     */
    @Override
    public String uploadWeChat(MultipartFile file) {
        OSSClientUtil ossClient = null;
        try{
            ossClient = new OSSClientUtil();
            String imgUrl = ossClient.uploadImg2Oss(file,"wechat");
            if("fail".equals(imgUrl)){
                return "上传失败";
            }
            return imgUrl;
        }catch (Exception e) {
            e.printStackTrace();
            return "上传失败";
        }
    }

    /**
     * 新增分享规则
     * @param appHomeBanner
     */
    @Override
    public void addShareRule(AppHomeBanner appHomeBanner) {
        appHomeBanner.setBannerType(27+"");
        appHomeBanner.setAddTime(new Date());
        appHomeBanner.setShowStatus(1);
        appHomeBanner.setSeq(0);
        appHomeBanner.setJumpType(6);
        appHomeBannerMapper.insert(appHomeBanner);
    }
}
